package cc.gu.tank.sprite;

import cc.gu.tank.Director;
import cc.gu.tank.enums.Direction;
import cc.gu.tank.enums.Group;
import cc.gu.tank.sound.SoundEffect;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;

import java.util.*;

/**
 * @Author: GuSongLiao
 * @Date: 2025/9/29 11:08
 * @Description: tank 类
 */
public class Tank extends Role {

    Random random = new Random();

    private static Map<Direction, Image> ownTankMap = new HashMap<Direction, Image>() {{
        put(Direction.UP, new Image("image/tank-green-up.png"));
        put(Direction.DOWN, new Image("image/tank-green-down.png"));
        put(Direction.LEFT, new Image("image/tank-green-left.png"));
        put(Direction.RIGHT, new Image("image/tank-green-right.png"));
    }};

    private static Map<Direction, Image> enemyTankMap = new HashMap<Direction, Image>() {{
        put(Direction.UP, new Image("image/tank-red-up.png"));
        put(Direction.DOWN, new Image("image/tank-red-down.png"));
        put(Direction.LEFT, new Image("image/tank-red-left.png"));
        put(Direction.RIGHT, new Image("image/tank-red-right.png"));
    }};

    public static double WIDTH = 60, HEIGHT = 60;

    /**
     * 子弹集合
     */
    public List<Bullet> bullets = new ArrayList<>();

    public Tank(Direction direction, double x, double y) {
        super(ownTankMap.get(direction), x, y, WIDTH, HEIGHT);
        this.direction = direction;
    }


    public Tank(Direction direction, double x, double y, Group group, boolean moveAble) {
        super(group == Group.OWNSIDE ? ownTankMap.get(direction) : enemyTankMap.get(direction), x, y, WIDTH, HEIGHT);
        this.group = group;
        this.direction = direction;
        this.moveAble = moveAble;

        System.out.println(String.format("创建坦克，坐标 x : %s, y : %s", x, y));
    }

    @Override
    public void move() {
        super.move();
        if (x < 0) x = 0;
        if (x + width > Director.WIDTH) x = Director.WIDTH - width;
        if (y < 0) y = 0;
        if (y + height > Director.HEIGHT) y = Director.HEIGHT - height;
        switch (direction) {
            case UP:
                image = group == Group.OWNSIDE ? ownTankMap.get(Direction.UP) : enemyTankMap.get(Direction.UP);
                break;
            case DOWN:
                image = group == Group.OWNSIDE ? ownTankMap.get(Direction.DOWN) : enemyTankMap.get(Direction.DOWN);
                break;
            case LEFT:
                image = group == Group.OWNSIDE ? ownTankMap.get(Direction.LEFT) : enemyTankMap.get(Direction.LEFT);
                break;
            case RIGHT:
                image = group == Group.OWNSIDE ? ownTankMap.get(Direction.RIGHT) : enemyTankMap.get(Direction.RIGHT);
                break;
        }
    }

    @Override
    public void draw(GraphicsContext graphicsContext2D) {
        super.draw(graphicsContext2D);
        enemyAction();
    }

    /**
     * 敌人移动 + 开火
     */
    public void enemyAction() {
        if (group == Group.ENEMY) {
            int i = random.nextInt(100);
            if (i > 98) {
                fire();
            }
            if (i > 95) {
                direction = Direction.values()[random.nextInt(Direction.values().length)];
            }
        }
    }


    public void fire() {
        if (bullets.size() <= 5) {
            SoundEffect.play("sound/attack.mp3");
            bullets.add(new Bullet(this));
        }
    }

    @Override
    public void pressed(KeyCode keyCode) {
        super.pressed(keyCode);
        switch (keyCode) {
            case J:
                fire();
        }
    }

    @Override
    public Sprite impact(List<? extends Sprite> sprites) {
        Sprite sprite = super.impact(sprites);
        if (sprite != null && !(sprite instanceof Tree)) {
            x = oldX;
            y = oldY;
        }
        //子弹处已经判断打中坦克的处理
//        //碰撞检测成功，对方为子弹，则销毁
//        if (sprite instanceof Bullet && this.group != ((Bullet) sprite).group) {
//            sprite.alive = false;
//            this.alive = false;
//        }
        return sprite;
    }
}
